package com.randi.system.service.impl.sysNotice;

import com.github.pagehelper.PageInfo;
import com.randi.common.config.AppConfig;
import com.randi.common.core.domain.entity.SysUser;
import com.randi.common.enums.ResultCode;
import com.randi.common.exception.ServiceException;
import com.randi.common.utils.SecurityUtils;
import com.randi.system.domain.entity.sysNotice.SysNotice;
import com.randi.system.domain.entity.sysNotice.SysNoticePush;
import com.randi.system.domain.params.SysUserParam;
import com.randi.system.mapper.sysNotice.SysNoticePushMapper;
import com.randi.system.service.sysNotice.ISysNoticePushService;
import com.randi.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysNoticePushServiceImpl implements ISysNoticePushService {

    @Resource
    private SysNoticePushMapper sysNoticePushMapper;

    @Resource
    private ISysUserService iSysUserService;

    @Resource
    private AppConfig.SystemConfig systemConfig;

    /**
     * 前置处理
     *
     * @param sysNotice 公告
     */
    @Override
    public void checkHandle(SysNotice sysNotice) {
        if (sysNotice == null || sysNotice.getDeleted() != 0) {
            throw new ServiceException(ResultCode.BUSINESS_ERROR_DETAIL, "公告不存在");
        } else if (sysNotice.getStatus() != 0) {
            throw new ServiceException(ResultCode.BUSINESS_ERROR_DETAIL, "公告已停用");
        }
    }

    /**
     * 获取已推送用户列表
     *
     * @param noticeId 公告ID
     * @return 已推送用户列表
     */
    @Override
    public List<Long> getPushUserIds(Long noticeId) {
        SysNoticePush sysNoticePush = new SysNoticePush();
        sysNoticePush.setNoticeId(noticeId);
        return sysNoticePushMapper.selectSysNoticePushList(sysNoticePush).stream()
                .map(SysNoticePush::getUserId)
                .collect(Collectors.toList());
    }

    /**
     * 分页获取推送用户列表
     *
     * @param userParam 用户参数
     * @return 用户列表
     */
    @Override
    public PageInfo<SysUser> selectUserPageList(SysUserParam userParam) {
        HashSet<Long> excludes = new HashSet<>();
        if (userParam.getExcludes() != null && !userParam.getExcludes().isEmpty()) {
            excludes.addAll(userParam.getExcludes());
        }
        excludes.add(SecurityUtils.getUserId());
        excludes.add(systemConfig.getUserAdminId());
        userParam.setExcludes(new ArrayList<>(excludes));
        return iSysUserService.selectPageList(userParam);
    }

    /**
     * 获取公告推送用户列表
     *
     * @param noticeId 公告ID
     * @return 用户列表
     */
    @Override
    public List<SysUser> selectUserList(Long noticeId) {
        List<Long> pushUserIds = getPushUserIds(noticeId);
        if (pushUserIds.isEmpty()) {
            return Collections.emptyList();
        }
        SysUserParam sysUserParam = new SysUserParam();
        sysUserParam.setIncludes(pushUserIds);
        return iSysUserService.selectSysUserList(sysUserParam);
    }


    /**
     * 检查公告推送是否存在
     *
     * @param noticeId 通告ID
     * @param userId   用户ID
     * @return 是否存在
     */
    @Override
    public Boolean checkSysNoticePushExist(Long noticeId, Long userId) {
        return sysNoticePushMapper.checkExist(noticeId, userId);
    }

    /**
     * 批量插入推送信息
     *
     * @param sysNoticePushes 推送信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchInsertSysNoticePush(List<SysNoticePush> sysNoticePushes) {
        return sysNoticePushMapper.batchInsert(sysNoticePushes);
    }
}
